<p>
  <b>Supplier:</b> <%=h @order.supplier.name %>
</p>
<% for column in Order.content_columns %>
<% if !['created_by'].include? column.name %>
<p>
  <% val = @order.send(column.name) %>
  <b><%= column.human_name %>:</b> <%= case column.name
                                         when 'state' then format_state val
                                         when 'orderer' then auto_link(h(val))
                                         when 'comments' then '<br/>' + auto_link(nl2br(h(val)))
                                         else h val
                                       end %>
</p>
<% end %>
<% end %>

<% if !@order.order_positions.empty? %>

<h2>Statistics</h2>
  <p>
    <b>Total worth:</b> <%= number_to_currency(@order.total_value, { :unit => 'PLN ', :precision => 2 }) %>
  </p>

  <table>
    <tr><th>Material</th><th>Quantity</th></tr>
  <% for entry in @order.material_stats %>
    <tr><td><%=h entry[0] %></td><td> <%=h entry[1] %> </td> </tr>
  <% end %>
  </table>
<% end %>

<h2>Order positions</h2>

<table>
<% for order_position in [nil] + @order.order_positions %>
  <tr>
  <% for column in OrderPosition.content_columns %>
    <% if !['created_by'].include? column.name %>
      <% if order_position.nil? %>
        <th><%= column.human_name %></th>
      <% else %>
        <td><%=h order_position.send(column.name) %></td>
      <% end %>
    <% end %>
  <% end %>
  <% if order_position.nil? %>
    <th>Material</th>
    <th colspan=3>Action</th>
  <% else %>
    <td><%=h order_position.material.name %></td>
    <td><%= link_to 'Show', :controller => 'order_positions', :action => 'show', :order => @order, :id => order_position %></td>
    <td><%= link_to 'Edit', :controller => 'order_positions', :action => 'edit', :order => @order, :id => order_position %></td>
    <td><%= link_to 'Destroy', { :controller => 'order_positions', :action => 'destroy', :order => @order, :id => order_position }, :confirm => 'Are you sure?', :method => :post %></td>
  <% end %>
  </tr>
<% end %>
</table>

<br />

<% if @order.state == Order::STATE_NEW %>
  <%= link_to 'New position', :controller => 'order_positions', :action => 'new', :order => @order %> |
  <% if !@order.order_positions.empty? %>
    <%= link_to 'Send order', { :action => 'send_order', :id => @order }, :method => :post, :confirm => 'Really send order?' %> |
  <% end %>
<% end %>  
<% if @order.state == Order::STATE_SENT || @order.state == Order::STATE_CONFIRMED %>
  <%= link_to 'Mark as received', { :action => 'received', :id => @order }, :method => :post, :confirm => 'You mean FOOD IS THERE?' %> 
<% end %>  

<hr/>

<%= link_to 'Edit', :action => 'edit', :id => @order %> |
<%= link_to 'Back', :action => 'list' %>
